package com.ruoyi.customer.service.impl;

import com.ruoyi.common.utils.SeqNumberUtils;
import com.ruoyi.customer.domain.Order;
import com.ruoyi.customer.domain.OrderNumber;
import com.ruoyi.customer.domain.Store;
import com.ruoyi.customer.mapper.StoreMapper;
import com.ruoyi.customer.service.IStoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 储存商品Service业务层处理
 * 
 * @author 王梦川
 * @date 2024-10-25
 */
@Service
public class StoreServiceImpl implements IStoreService 
{
    @Autowired
    private StoreMapper storeMapper;

    @Override
    public int updateOrderForYmy(Order order) {
        return storeMapper.updateOrderForYMY(order);
    }

    /**
     * 查询储存商品
     * 
     * @param id 储存商品主键
     * @return 储存商品
     */
    @Override
    public Store selectStoreById(Long id)
    {
        return storeMapper.selectStoreById(id);
    }

    /**
     * 查询储存商品列表
     * 
     * @param order 储存商品
     * @return 储存商品
     */
    @Override
    public List<Order> selectStoreList(Order order)
    {
        return storeMapper.selectStoreList(order);
    }

    /**
     * 新增储存商品
     * 
     * @param store 储存商品
     * @return 结果
     */
    @Transactional
    @Override
    public int insertStore(Store store)
    {
        storeMapper.insertContract(store);
        store.setStatus("0");
        store.setOrderNo(SeqNumberUtils.getRKBDNum());
        storeMapper.insertOrder(store);
        List<OrderNumber> orderNumber = store.getOrderNumber();
        int i = 0;
        for (OrderNumber order : orderNumber) {
            order.setOrderNo(store.getOrderNo());
            storeMapper.insertOrderNumber(order);
            i++;
        }
        return i;
    }

    /**
     * 修改储存商品
     * 
     * @param store 储存商品
     * @return 结果
     */
    @Transactional
    @Override
    public int updateStore(Store store)
    {
        int i = 0;
        System.out.println(store);
        storeMapper.updateOrder(store);
        storeMapper.updateContract(store);
        List<OrderNumber> orderNumber = store.getOrderNumber();
        for (OrderNumber order : orderNumber) {
            storeMapper.updateOrderNumber(order);
            i++;
        }
        return i;
    }

    /**
     * 批量删除储存商品
     * 
     * @param store 需要删除的储存商品主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteStoreByIds(Store store)
    {
        List<OrderNumber> orderNumber = store.getOrderNumber();
        for (OrderNumber order : orderNumber) {
            storeMapper.deleteOrderNumber(order);
        }
        storeMapper.deleteContractById(Long.valueOf(store.getContractId()));
        return storeMapper.deleteStore(store);
    }

}
